﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace map.common
{
    public partial class UploadFilePage : System.Web.UI.Page
    {
        #region 文件上传相关字段和属性
        private static string _fileType = ".jpg|.gif";
        private static string _uploadPath = "UploadFiles/";
        private static int _maxFilesQuantity = 10;

        public static string FileType
        {
            get { return _fileType; }
            set { _fileType = value; }
        }

        public static string UploadPath
        {
            get { return _uploadPath + DateTime.Now.ToString("yyyy_MM_dd") + "/"; }
            set { _uploadPath = value; }
        }

        public static int MaxFilesQuantity
        {
            get { return _maxFilesQuantity; }
            set { _maxFilesQuantity = value; }
        }
        #endregion

        #region 文件上传辅助方法
        /// <summary>
        /// Check the file's extension is valid or not.
        /// </summary>
        private static bool CheckExtensionName(string fileName)
        {
            if (FileType == ".*") return true;      

            if (FileType.IndexOf(System.IO.Path.GetExtension(fileName).ToLower()) == -1)
                return false;
            else
                return true;
        }

        /// <summary>
        /// Generate a new filename from the old filename.
        /// </summary>
        private static string GenerateFileName(string fileName)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(new Random().Next(100, 999).ToString());
            sb.Append("_");
            sb.Append(DateTime.Now.ToString("HHmmss"));
            sb.Append("_");
            sb.Append(fileName);

            return sb.ToString();
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void SaveFileButton_Click(object sender, EventArgs e)
        {
            string clientFileName = string.Empty;
            string serverFileUrl = string.Empty;
            string errorMsg = string.Empty;
            if (this.MainFileUpload.HasFile)
            {
                clientFileName = this.MainFileUpload.PostedFile.FileName;

                string fileName = GenerateFileName(System.IO.Path.GetFileName(this.MainFileUpload.FileName));
                string uploadPath = UploadPath;
                string filePath = HttpContext.Current.Server.MapPath("/" + uploadPath);
                if (CheckExtensionName(fileName))
                {
                    if (this.MainFileUpload.PostedFile.ContentLength > 120 * 1024)
                    {
                        errorMsg = "图片文件太大，应该小于120K";
                    }
                    else
                    {
                        if (!System.IO.Directory.Exists(filePath))
                            System.IO.Directory.CreateDirectory(filePath);
                        this.MainFileUpload.SaveAs(filePath + fileName);
                        serverFileUrl = ResolveUrl("~/" + uploadPath + fileName);

                        this.HttpFileUrlHidden.Value = serverFileUrl;
                    }
                }
                else
                {
                    errorMsg = "文件格式不正确，应该为jpg或gif格式";
                }
            }

            this.FileNameHidden.Value = clientFileName ;
            this.CheckedUploadFileHidden.Value = "1";
            this.UploadFileErrorMsgHidden.Value = errorMsg;
        }
    }
}
